<?php

class EvaluationAction extends Action {


		// 商品评论列表
		public function getEvaluationList($search=NULL,$proname=NULL,$username=NULL,$commondesc=NULL,$orderstate=NULL,$start=0,$limit=10){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			$customerId = Session::$user['customerId'];
			Session::switchCustomer(0);
			$commentModel = M("module_product_comment");
			Session::switchCustomer($customerId);
            if ($search !==NULL && $search !== '') {
                $where['mpro.name'] =  array("like","%$search%");
                $where['uinfo.nickname'] =  array("like","%$search%");
                $where['comment.comment'] =  array("like","%$search%");
                $where['_logic'] = "OR";
                $condition["_complex"] = $where;
            }
			$condition['comment.deleted'] = 0;
			$condition['comment.fromCustomerId'] = $customerId;
			 if($orderstate !==NULL && $orderstate !== ''){
				$condition['pro.status'] = array("like","%$orderstate%");
			}

			$arr=$commentModel
				->alias("comment")
				->join("wx_".$customerId."_user_weixin_info uinfo on uinfo.openid = comment.fromUserName")
				->join("RIGHT JOIN wx_module_product_order pro on pro.id = comment.oid")
                ->join("wx_quan_user quan on pro.qId=quan.qid and pro.miUid=quan.uid")
				->join("wx_module_product mpro on comment.pid = mpro.id")
				->where($condition)
				->order("comment.createTime DESC")
				->group("comment.oid,comment.pid")
				->limit($start, $limit)
				->field('comment.id,comment.pid,comment.oid,comment.comment,comment.imgurl,comment.createTime,comment.redMoney,comment.type, pro.serialNumber,pro.status,mpro.name,uinfo.nickname as userfullname,uinfo.headimgurl,quan.nickName as quanName,quan.headImgUrl as quanHead')
				->select();
			foreach($arr as $key=>$value){
			    $commentInfo=null;
			    if($value['type']==1){
                    $arr[$key]['communityId']=$value['id'];
                    $arr[$key]['communityComment']=$value['comment'];
                    $arr[$key]['communityimgs']=$value['imgurl'];
                    $arr[$key]['communityCreateTime']=$value['createTime'];
                    $arr[$key]['communityRedMoney']=$value['redMoney'];
                    $commentInfo = $commentModel->where("pid=".$value['pid']." and oid=".$value['oid']." and type=0")->field("id,comment,imgurl,createTime,redMoney")->find();
                    $arr[$key]['id']=$commentInfo['id'];
                    $arr[$key]['comment']=$commentInfo['comment'];
                    $arr[$key]['imgurl']=$commentInfo['imgurl'];
                    $arr[$key]['createTime']=$commentInfo['createTime'];
                    $arr[$key]['redMoney']=$commentInfo['redMoney'];
                }else{
                    $commentInfo = $commentModel->where("pid=".$value['pid']." and oid=".$value['oid']." and type=1")->field("id,comment,imgurl,createTime,redMoney")->find();
                    $arr[$key]['communityId']=$commentInfo['id'];
                    $arr[$key]['communityComment']=$commentInfo['comment'];
                    $arr[$key]['communityimgs']=$commentInfo['imgurl'];
                    $arr[$key]['communityCreateTime']=$commentInfo['createTime'];
                    $arr[$key]['communityRedMoney']=$commentInfo['redMoney'];
                }
				if($arr[$key]["imgurl"]){
					$arr[$key]["imgurl"] = explode(",",$arr[$key]["imgurl"]);
				}
                if($arr[$key]["communityimgs"]){
                    $arr[$key]["communityimgs"] = explode(",",$arr[$key]["communityimgs"]);
                }
			}
			if ($arr) {
                $count=$commentModel
                    ->alias("comment")
                    ->join("wx_".$customerId."_user_weixin_info uinfo on uinfo.openid = comment.fromUserName")
                    ->join("RIGHT JOIN wx_module_product_order pro on pro.id = comment.oid")
                    ->join("wx_quan_user quan on pro.qId=quan.qid and pro.miUid=quan.uid")
                    ->join("wx_module_product mpro on comment.pid = mpro.id")
                    ->where($condition)
                    ->group("comment.oid,comment.pid")
                    ->field('comment.id')
                    ->select();
				$count=count($count);
				Output::instance()->objArray($arr,$count);
			} else {
				Output::instance()->resultCode(code_success);
			}	
		}
		//某一商品的评论
		public function commentOfProduct($pid,$start=0,$limit=10){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			$customerId = Session::$user['customerId'];
			Session::switchCustomer(0);
			$commentModel = M("module_product_comment");
			$relationMoel = M("module_comment_relation_product");
			Session::switchCustomer($customerId);
			$where['_string']="a.pid = $pid or b.pid=$pid";
			$where['a.fromCustomerId']=array("eq",$customerId);
			$where['a.deleted']=array("eq",0);
			$arr = $commentModel->alias("a")
				->join("wx_module_comment_relation_product b on a.pid=b.relationPid")
				->join("inner join wx_".$customerId."_user_weixin_info c on a.fromUserName=c.openid")
				->where($where)
				->field("a.id,a.comment,a.imgurl,c.nickname,c.headimgurl,a.createTime")
				->limit($start,$limit)
				->order("a.createTime desc")
				->select();
			if($arr){
				$count = $commentModel->alias("a")
					->join("wx_module_comment_relation_product b on a.pid=b.relationPid")
					->join("inner join wx_".$customerId."_user_weixin_info c on a.fromUserName=c.openid")
					->where($where)
					->count();
				Output::instance()->objArray($arr,$count);
			}else{
				Output::instance()->resultCode();
			}

		}
		//添加公用商品评论
		public function relationProductComment($pid,$pids){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			$customerId = Session::$user['customerId'];
			Session::switchCustomer(0);
			$relationModel = M("module_comment_relation_product");
			Session::switchCustomer($customerId);
			$pids = explode(",",$pids);
			foreach($pids as $key=>$value){
				$data['pid']=$pid;
				$data['relationPid']=$value;
				$data['createTime']=date("Y-m-d H:i:s");
				$relationModel->add($data);
			}
			Output::instance()->resultCode();
		}
		//删除公用评论的商品
		public function delProductRelation($id){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			$customerId = Session::$user['customerId'];
			Session::switchCustomer(0);
			$relationModel = M("module_comment_relation_product");
			Session::switchCustomer($customerId);
			$result = $relationModel->where("id=$id")->delete();
			if($result!==false){
				Output::instance()->resultCode();
			}else{
				Output::instance()->resultCode(code_sql_error);
			}
		}
		//查看已经共用评论的商品
		public function relationProducts($pid,$start=0,$limit=10){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			$customerId = Session::$user['customerId'];
			Session::switchCustomer(0);
			$relationModel = M("module_comment_relation_product");
			Session::switchCustomer($customerId);
			$arr = $relationModel->alias("a")
				->join("inner join wx_module_product b on a.relationPid=b.id")
				->where("a.pid=$pid")
				->field("a.id,b.name,b.picUrl,a.createTime")
				->limit($start,$limit)
				->order("a.createTime desc")
				->select();
			if($arr){
				$count = $relationModel->alias("a")
					->join("inner join wx_module_product b on a.relationPid=b.id")
					->where("a.pid=$pid")->count();
				Output::instance()->objArray($arr,$count);
			}else{
				Output::instance()->resultCode();
			}

		}
		//所有商品
		public function getAllProduct($serialNumber=null,$name=null,$isRemoved=null,$start=0,$limit=10){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			$customerId = Session::$user['customerId'];
			Session::switchCustomer(0);
			$ProductModel = M("module_product");
			Session::switchCustomer($customerId);
			$where['fromCustomerId']=array('eq',$customerId);
			$where['deleted']=array("eq",0);
			$serialNumber?$where['serialNumber']=array("like","%$serialNumber%"):"";
			$isRemoved?$where['isRemoved']=array("eq",$isRemoved):"";
			$name?$where['name']=array("like","%$name%"):"";
			$arr = $ProductModel->where($where)->field("id,name,picUrl,isRemoved,serialNumber")
				->limit($start,$limit)->order("updateTime desc")->select();
			if($arr){
				$count = $ProductModel->where($where)->count();
				Output::instance()->objArray($arr,$count);
			}else{
				Output::instance()->resultCode();
			}
		}
		//添加共用评论的商品
		public function addRelationProduct($id,$pids){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			$customerId = Session::$user['customerId'];
			Session::switchCustomer(0);
			$relationModel = M("module_comment_relation_product");
			Session::switchCustomer($customerId);
			$pids = explode(",",$pids);
			$data['pid']=$id;
			$data['createTime']=date("Y-m-d H:i:s");
			foreach($pids as $key=>$value){
				$relationInfo = $relationModel->where("pid=$id and relationPid=$value")->find();
				if(!$relationInfo&&$value!=$id){
					$data['relationPid']=$value;
					$relationModel->add($data);
				}
			}
			Output::instance()->resultCode();
		}
		// 屏蔽评论
		public function ShieldComment($id){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			$customerId = Session::$user['customerId'];
			Session::switchCustomer(0);
			$commentModel = M("module_product_comment");
			$ProductModel = M("module_product");
			Session::switchCustomer($customerId);

			$condition['com.id']=$id;
			$condition['com.fromCustomerId']=$customerId;
			// 扣减商品评论数
			$procount = $commentModel->alias("com")
						->join("wx_module_product_order_detail detail ON com.oid=detail.oid")
						->join("wx_module_product product ON detail.pid=product.id")
						->where($condition)
						->field("product.id,product.commentCount")
						->find();
			if ($procount) {
				if ($procount['commentCount'] && $procount['commentCount']>0) {
						$prodata['commentCount']=$procount['commentCount']-1;
				}else{
						$prodata['commentCount'] = 0;
				}
				$procomwhere['id']=$procount['id'];
				$result=$ProductModel->where($procomwhere)->save($prodata);
			}
			$data['deleted'] = 1;
			$result=$commentModel->alias("com")->where($condition)->save($data);
			// echo $commentModel->getlastSql();
			if($result!==false){
				Output::instance()->resultCode();
			}else{
				Output::instance()->resultCode(code_success);
			}

		}
		public function getBackInfoById($id){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			$customerId = Session::$user['customerId'];
			Session::switchCustomer(0);
			$commentModel = M("module_product_comment");
			Session::switchCustomer($customerId);
			$result = $commentModel->where("id=".$id." and fromCustomerId = ".$customerId)->find();
			Output::instance()->object($result);
		}
		public function backRedMoney($id,$wishes,$redMoney){
			$retVal = Session::init();
			if (!$retVal) {
				Output::instance()->resultCode(code_access_denied);
				return;
			}
			$customerId = Session::$user['customerId'];
			Session::switchCustomer(0);
			$commentModel = M("module_product_comment");
			Session::switchCustomer($customerId);
			$mallStore = M("module_mall_store");
			$setting = R("CUser/getMallSetting");
			if (!$setting) {
				return;
			}
			$mallInfo = $mallStore->find();
			$data["wishes"] = $wishes;
			$condition["id"] = $id;
			$condition["fromCustomerId"]=$customerId;
			$commentInfo = $commentModel->where($condition)->find();
			$result = WeixinApi::sendRedEnvelopeByOpenId(
						$mallInfo["name"],
						$commentInfo["fromUserName"],
						$redMoney,
						$wishes,
						true,
						$setting["payKey"],
						$setting["mchId"],
						$setting["appId"]
					);
			if($commentInfo['redMoney']){
				$redMoney+=$commentInfo['redMoney'];
			}
			$data["redMoney"] = $redMoney;
			if($result){
				 $commentModel->where($condition)->save($data);
			}
			return;
		}
		//获得晒单配置
        public static function getCommentConfig(){
            $retVal = Session::init();
            if (!$retVal) {
                Output::instance()->resultCode(code_access_denied);
                return;
            }
            $customerId = Session::$user['customerId'];
            Session::switchCustomer(0);
            $commentConfig = M("module_comment_config");
            Session::switchCustomer($customerId);
            $result = $commentConfig->where("customerId=$customerId")->find();
            empty($result) ? $result=array() : '';
            Output::instance()->object($result);
        }
    //设置晒单配置参数
    public static function setCommentConfig($singleProductAmount,$validityDays,$mallAutomatic,$mallRedMoney,$mallRedWish,$communityAutomatic, $communityRedMoney,$communityRedWish){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $commentConfig = M("module_comment_config");
        Session::switchCustomer($customerId);
        $where['customerId']=array("eq",$customerId);
        $id = $commentConfig->where($where)->getField("id");
        $data['singleProductAmount']=$singleProductAmount;
        $data['validityDays']=$validityDays;
        $data['mallAutomatic']=$mallAutomatic;
        $data['mallRedMoney']=$mallRedMoney;
        $data['mallRedWish']=$mallRedWish;
        $data['communityAutomatic']=$communityAutomatic;
        $data['communityRedMoney']=$communityRedMoney;
        $data['communityRedWish'] = $communityRedWish;
        if($id){
            $commentConfig->where($where)->save($data);
        }else{
            $data['customerId']=$customerId;
            $commentConfig->where($where)->add($data);
        }
        Output::instance()->resultCode();
    }

}